<?php
namespace Admin\Controller;

class DcategoryController extends AdminBaseController
{
   public function index()
    {
        if(session('auid')==null){
            $this->display('index/login');
            exit();
        }
        $this->display('dcategory/index');
    }
    
    public function dlist(){
        if(session('auid')==null){
            $this->display('index/login');
            exit();
        }
        $name=I('name','');
        if($name!=''){
            $condi['name']=array('like', '%'.$name);
        }
        
        $page=I('page',1);
        $rows=I('rows',50);
        $start=($page-1)*$rows;
       
        $DomainType = M('DomainType');
        $list = $DomainType->where($condi)->order("createdate desc")->limit($start,$rows)->select();
        $count = $DomainType->where($condi)->count();
        $map=array(
            'rows'=>$list,
            'total'=>$count
        );
        $this->ajaxReturn($map,'JSON');
    }
    
    public function tosave(){
        if(session('auid')==null){
            $this->display('index/login');
            exit();
        }
      $this->display("dcategory/save");
    }
    
    public function save(){
        $dtype=M('DomainType');
        $data['name']=I('name','');
        $old = $dtype->where($data)->select();
        if(!empty($old)){
            $map=array(
                'status'=>2
            );
            $this->ajaxReturn($map,'JSON');
            exit();
        }
        $data['is_entrust']=I('is_entrust','');
        $data['credit_limit']=I('credit_limit','');
        $data['refix']=I('refix','');
        $data['createdate']=date("Y-m-d");
        
        $dtype->add($data);
        $map=array(
            'status'=>1
        );
        $this->ajaxReturn($map,'JSON');
    }
    
    
    public function update(){
        if(session('auid')==null){
            $this->display('index/login');
            exit();
        }
        $id=I('id','');
        $cate=M('DomainType')->find($id);
        $this->ajaxReturn($cate,'JSON');
    }
    
    public function toupdate(){
        if(session('auid')==null){
            $this->display('index/login');
            exit();
        }
        $data['name']=I('name','');
        $data['is_entrust']=I('is_entrust','');
        $data['credit_limit']=I('credit_limit','');
        $data['refix']=I('refix','');
        M('DomainType')->where('id='.I('id',''))->save($data);
        $map=array(
            'status'=>1
        );
        $this->ajaxReturn($map,'JSON');
    }
    
    public function upstatus(){
        if(session('auid')==null){
            $this->display('index/login');
            exit();
        }
        $data['status']=I('status','');
        M('DomainType')->where('id='.I('id',''))->save($data);
        $map=array(
            'status'=>1
        );
        $this->ajaxReturn($map,'JSON');
    }
}